package com.tsystems.dao;

import javax.persistence.EntityTransaction;

public abstract class AbstractDAO<T> {
	public void insert(T entity) {
		EntityTransaction trx = TrainsEntityManager.getInstance()
				.getTransaction();
		try {
			trx.begin();
			TrainsEntityManager.getInstance().persist(entity);
			trx.commit();
		} finally {
			if (trx.isActive()) {
				trx.rollback();
			}
		}
	}
	
	public void remove(T entity) {
		EntityTransaction trx = TrainsEntityManager.getInstance()
				.getTransaction();
		try {
			trx.begin();
			TrainsEntityManager.getInstance().remove(entity);
			trx.commit();
		} finally {
			if (trx.isActive()) {
				trx.rollback();
			}
		}

	}

	public void update(T entity) {
		EntityTransaction trx = TrainsEntityManager.getInstance()
				.getTransaction();
		try {
			trx.begin();
			TrainsEntityManager.getInstance().merge(entity);
			trx.commit();
		} finally {
			if (trx.isActive()) {
				trx.rollback();
			}
		}
	}
}
